import Vue from 'vue';
import VueRouter from 'vue-router';
import { get } from '@/common/utils';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/login/Login.vue'),
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import(/* webpackChunkName: "Admin" */ '@/views/admin/Admin.vue'),
  },
  {
    path: '/menu/:id',
    name: 'Menu',
    component: () => import(/* webpackChunkName: "Menu" */ '@/views/menu/Menu.vue'),
  },
  {
    path: '/restaurant',
    name: 'Restaurant',
    component: () => import(/* webpackChunkName: "Restaurant" */ '@/views/restaurant/Restaurant.vue'),
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import(/* webpackChunkName: "Order" */ '@/views/order/Order.vue'),
    beforeEnter(from, to, next) {
      const user = get('user');
      if (user) {
        if (user.token) {
          next();
        }
      } else {
        next('/restaurant');
      }
    },
  },
  {
    path: '/',
    redirect: '/restaurant',
  },
  {
    path: '*',
    component: () => import(/* webpackChunkName: "404" */ '@/views/404page/404Page.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;
